/**
 * Copyright (C), 2021, 中电福富信息科技有限公司
 * FileName: MapUtil
 * Author:   xujiajun
 * Date:     2021/2/1 11:19
 */
package com.example.demo.web.rest.util;

import com.ffcs.helper.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * @Author xujiajun
 * @Date 2021/2/1  11:19
 */
public class MapUtil {

    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, Object> objectToMap(Object obj) {
        Map<String, Object> map = new HashMap<String,Object>();
        try {
            Class<?> clazz = obj.getClass();
            for (Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                String fieldName = field.getName();
                Object value = StringUtils.nvl(String.valueOf(field.get(obj)));
                map.put(fieldName, value);
            }
        }catch (Exception e) {
            e.printStackTrace();
            return map;
        }
        return map;
    }
}